$(function(){
	
	//------------- Date time picker -------------//
	$(".datetime-picker2").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-left",
        minView:2
    });
	
	getCode();
	
	$("#password").formwizard({ 
	 	formPluginEnabled: true,
	 	validationEnabled: true,
	 	validationOptions: {
	 		errorPlacement: function(error, element) {
				wrap = element.parent();
				wrap1 = wrap.parent();
				if (wrap1.hasClass('checkbox')) {
					error.insertAfter(wrap1);
				} else {
					error.insertAfter(element);
				}
			}, 
			errorClass: 'help-block',
	 		rules: {
	 			firstname: {
	 				required: true
	 			},
	 			email: {
	 				required: true,
	 				email: true
	 			},
	 			username: {
	 				required: true
	 			},
	 			password: {
	 				required: true,
	 				minlength: 5
	 			},
	 			password_2: {
	 				required: true,
					minlength: 5,
					equalTo: "#password"
	 			},
	 		}, 
	 		messages: {
	 			firstname: {
	 				required: "I need to know your first name Sir"
	 			},
	 			email: {
	 				required: "You email is required Sir"
	 			}
	 		},
			highlight: function(element) {
				if ($(element).offsetParent().parent().hasClass('form-group')) {
					$(element).offsetParent().parent().removeClass('has-success').addClass('has-error');
				} else {
					$(element).offsetParent().parent().parent().parent().removeClass('has-success').addClass('has-error');
				}
		    },
		    unhighlight: function(element,errorClass) {
		    	if ($(element).offsetParent().parent().hasClass('form-group')) {
		    		$(element).offsetParent().parent().removeClass('has-error').addClass('has-success');
			    	$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		    	} else if ($(element).offsetParent().parent().hasClass('checkbox')) {
		    		$(element).offsetParent().parent().parent().parent().removeClass('has-error').addClass('has-success');
		    		$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		    	} else {
		    		$(element).offsetParent().parent().parent().removeClass('has-error').addClass('has-success');
		    	}
			}
	 	},
	 	focusFirstInput : true,
	 	formOptions :{
			success: function(data){
				//produce success message
				createSuccessMsg($("#wizard2 .msg"), "You successfully submit this form");
			},
			resetForm: false
	 	},
	 	disableUIStyles: true,
	 	showSteps: true //show the step
	});	
})

function updateUser(){
	layer.confirm('Are you sure to update your information?',{btn:['yes','no']},function(){
		$.ajax({
			type:"post",
			url:"updateUser",
			data:$("#updateForm").serialize(),
			async:false,
			beforeSend:function(){
				var flag=$("#code").attr("data-flag");
				if(flag=="right"){
					return true;
				}else{
					layer.close(layer.index);
					$("#code").focus();
					return false;
				}
			},
			success:function(res){
				if(res =="0000"){
					layer.msg('update success!',{icon:1,time:1000});
					location.reload();
				}else{
					layer.msg('update fail!Please try again',{icon:2,time:1000});
				}
			},
			error:function(err){
				layer.msg('update fail:'+err.status,{icon:2,time:1000});
			}
		});
	});
}

function getCode(){
	$("#imageCode").attr("src","getImgCode?time="+new Date());
}

function checkCode(code){
	var url = "checkImg";
	    $.get(url,{"code":code},function(data){
	         if(data=="0000"){
	        	$( "#codeTip").html("<font class='color-green'>√ ok~</font>");
	        	$("#code").attr("data-flag","right");
	        }else{
	        	$("#codeTip").html("<font color='#f68484'>× code is wrong</font>");
	        	$("#code").attr("data-flag","wrong");
	        	getCode(); 
	        }
	    }) 
}
